﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace LinkViewer
{
    /// <summary>
    /// Interaction logic for winExtractLinksProgress.xaml
    /// </summary>
    public partial class winExtractLinksProgress : Window
    {
        List<jcc.com.dailyez.Link> links = new List<jcc.com.dailyez.Link>();
        BackgroundWorker workerBee = new BackgroundWorker();
        LinkWrapper wrapper = new LinkWrapper();

        public winExtractLinksProgress(int folderID)
        {
            InitializeComponent();
            lblStatus.Content = "Extracting Links";

           
            //add events and set properties of the worker
            workerBee.DoWork += new DoWorkEventHandler(workerBee_DoWork);
            workerBee.WorkerReportsProgress = true;
            workerBee.ProgressChanged += new ProgressChangedEventHandler(workerBee_ProgressChanged);
            workerBee.RunWorkerCompleted += new RunWorkerCompletedEventHandler(workerBee_RunWorkerCompleted);

            //start the worker
            workerBee.RunWorkerAsync(folderID);
            App.LastFolderOpened = folderID;
        }

        void workerBee_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            
            if (e.Cancelled == true)
            {
                this.Close();
            }
            else
            {            
                winLinkViewer win = new winLinkViewer(wrapper);
                win.Show();
                this.Close();
            }
        }

        void workerBee_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progBar.Value = e.ProgressPercentage;
        }

        void workerBee_DoWork(object sender, DoWorkEventArgs e)
        {
            //get the folderID passed in as an argument
            int id = (int)e.Argument;


            //get the pages from the folder
            lblStatus.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { lblStatus.Content = "Extracting links from pages "; }));
            jcc.com.dailyez.Page[] pages = jcc.StandardWebService.WebService.GetAllPages(jcc.StandardWebService.GetKey(), id);
            

            double counter = 0;
            double pageCount = pages.Length;
            foreach (jcc.com.dailyez.Page p in pages)
            {
                links.AddRange(jcc.StandardWebService.WebService.GetLinksFromPage(jcc.StandardWebService.GetKey(), p.ID));
                counter++;

                double f = counter / pageCount;
                if (f == 1)
                {
                    lblStatus.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { lblStatus.Content = "Extracting links complete.  Preparing links for viewer."; }));


                    foreach (jcc.com.dailyez.Link link in links)
                    {
                        if (link.URL.Length == 0)
                            continue;
                        //since these are directory links, use the DirectoryLink object, fill in the info, and add it to the wrapper object
                        DirectoryLink dLink = new DirectoryLink();
                        dLink.Link = link;
                        wrapper.Add(dLink);                    
 
                    }
                }
                workerBee.ReportProgress((int)(f * 100));
            }

            //log the opening of the directory
            jcc.StandardWebService.WebService.LogLinkViewerEvent(jcc.StandardWebService.GetKey(), DateTime.Now, jcc.Login.Username, id, 0, 0, "{Date} - {Time} {Username} opened folder: {Folder}");
        }
    }
}
